<!--
 * @Author: daifen
 * @Date: 2020-06-29 13:53:26
 * @LastEditors: daifen
--> 
<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item label="旧签名密码" prop="oldSigPassword">
      <el-input v-model="user.oldSigPassword" placeholder="请输入旧签名密码" type="SigPassword" />
    </el-form-item>
    <el-form-item label="新签名密码" prop="newSigPassword">
      <el-input v-model="user.newSigPassword" placeholder="请输入新签名密码" type="SigPassword" />
    </el-form-item>
    <el-form-item label="确认签名密码" prop="confirmSigPassword">
      <el-input v-model="user.confirmSigPassword" placeholder="请确认签名密码" type="SigPassword" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">保存</el-button>
      <el-button type="danger" size="mini" @click="close">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

import {updateUserSigPwd} from "@/api/system/user";

export default
{
  data(){
    const equalToSigPassword = (rule, value, callback) => {
      if (this.user.newSigPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      test: "1test",
      user: {
        oldSigPassword: undefined,
        newSigPassword: undefined,
        confirmSigPassword: undefined
      },
      // 表单校验
      rules: {
        oldSigPassword: [
          {required: true, message: "旧签名密码不能为空", trigger: "blur"}
        ],
        newSigPassword: [
          {required: true, message: "新签名密码不能为空", trigger: "blur"},
          {min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur"}
        ],
        confirmSigPassword: [
          {required: true, message: "确认签名密码不能为空", trigger: "blur"},
          {required: true, validator: equalToSigPassword, trigger: "blur"}
        ]
      }
    };
  },
  methods: {
    submit(){
      console.log(123);
      this.$refs["form"].validate(valid => {
      if (valid) {
        updateUserSigPwd(this.user.oldSigPassword, this.user.newSigPassword).then( response => {
        if (response.code === 200) {
          this.msgSuccess("修改成功");
        } else {
          this.msgError(response.msg);
        }
      } )
      }
    })
    },
    close(){
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({path: "/index"});
    }
  }
}
</script>
